package beans;

import java.io.Serializable;
import java.util.*;
import java.sql.*;
import toolbox.DBconne;

public class VmInfoBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private String ID,USERID,VMNAME,OS,VCPU,RAM,DISK,status,IP;
	public String getID() {
		return ID;
	}
	public void setID(String id) {
		ID = id;
	}
	public String getUSERID() {
		return USERID;
	}
	public void setUSERID(String userid) {
		USERID = userid;
	}
	public String getVMNAME() {
		return VMNAME;
	}
	public void setVMNAME(String vmname) {
		VMNAME = vmname;
	}
	public String getOS() {
		return OS;
	}
	public void setOS(String os) {
		OS = os;
	}
	public String getDISK() {
		return DISK;
	}
	public void setDISK(String disk) {
		DISK = disk;
	}
	public String getVCPU() {
		return VCPU;
	}
	public void setVCPU(String vcpu) {
		VCPU = vcpu;
	}
	public String getRAM() {
		return RAM;
	}
	public void setRAM(String ram) {
		RAM = ram;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	public String getIP() {
		return IP;
	}
	public void setIP(String ip) {
		IP = ip;
	}
	public static String queryIpAddr(int VMId){
		String sql="select IP from MACIP where VMID=?";
		Connection con = DBconne.getConnection();
		PreparedStatement pstmt = null;
		String ipAddr = null;
		ResultSet rs = null;
		try{
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, VMId);
			rs = pstmt.executeQuery();
			if(rs==null) System.out.println("Can't find the IP address of "+
					"VM "+VMId);
			while(rs.next()){
				ipAddr = rs.getString("IP");
			} 
		}catch (SQLException e){
				System.out.println("SQLException!");
		} finally{
				try{
					if(pstmt!=null) pstmt.close();
					DBconne.closeConnection();
				} catch (SQLException e){}
		}
		return ipAddr;	
	}
	public static int getUserId(String userName){
		String sql="select ID from User where USER=?";
		Connection con = DBconne.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int userId = -1;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, userName);
			rs = pstmt.executeQuery();
			if(rs == null) System.out.println("Can't find the user "+userName);
			if(rs.next()) userId = rs.getInt("ID");
		} catch (SQLException e){}
		finally{
			try{
				if(pstmt!=null) pstmt.close();
				DBconne.closeConnection();
			}catch(SQLException e){}
		}
		return userId;
	}
	public static boolean isRunning(String VMName){
		String sql="select * from OnlineVMS where VMNAME=?";
		Connection con = DBconne.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, VMName);
			rs = pstmt.executeQuery();
			if(rs == null) System.out.println("Can't judge whether VM "
					+VMName+"is running.");
			if(rs.next()) return true;
		} catch (SQLException e){}
		finally{
			try{
				if(pstmt!=null) pstmt.close();
				DBconne.closeConnection();
			}catch(SQLException e){}
		}
		return false;
	}
	public static ArrayList<VmInfoBean> getAllVmInfo(String userName){
		ArrayList<VmInfoBean> list = new ArrayList<VmInfoBean>();
		String sql="select VMDetail.ID,USERID,VMNAME,OS,VCPU,RAM,DISK,IP from VMDetail,MACIP "
			+"where USERID=? and VMDetail.ID=MACIP.VMID";
		
		int userId = getUserId(userName);
		if(userId<1){
			System.out.println("User does not exist!");
			return null;
		}
		
		/****debug****/
		System.out.println("UserId is:"+userId+" (VmInfoBean.java line 144)");
		/*************/
		
		
		Connection con = DBconne.getConnection();
		
		PreparedStatement pstmt = null;
		
		ResultSet rs = null;
		try{
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, userId);
			rs = pstmt.executeQuery();
			if(rs == null) System.out.println("Wrong here!(VmInfoBean.java line 148)");
			while(rs.next()){
				VmInfoBean vm = new VmInfoBean();
				vm.setID(rs.getString("ID"));
				vm.setUSERID(rs.getString("USERID"));
				String fullVMName = rs.getString("VMNAME");
				vm.setVMNAME(fullVMName.substring(fullVMName.indexOf('_')+1,fullVMName.length()));
				vm.setOS(rs.getString("OS"));
				vm.setVCPU(rs.getString("VCPU"));
				vm.setRAM(rs.getString("RAM"));
				vm.setDISK(rs.getString("DISK"));
				if(isRunning(fullVMName))vm.setStatus("Running");
				else vm.setStatus("Powered off");
				vm.setIP(queryIpAddr(Integer.valueOf(vm.getID())));
				list.add(vm);
			}
			
			
			/****debug****/
			System.out.println("User has "+list.size()+" VM(s) (VmInfoBean.java line 176)");
			/*************/
			
			
		} catch (SQLException e){}
		finally{
			try{
				if(pstmt!=null) pstmt.close();
				DBconne.closeConnection();
			} catch (SQLException e){}
		}
		return list;
	}
	
}
